package HouseRent.view;

import HouseRent.domain.House;
import HouseRent.service.HouseService;
import HouseRent.utils.Utility;

/**
 * @Package: houserent.view
 * @ClassName: HouseViews
 * @Author: x2606
 * @CreateTime: 2022/3/4 18:45
 * @Description: 显示层，1.显示界面，2.接受用户输入，3.调用HouseServise完成对房屋的操作
 */

public class HouseViews {

    private boolean loop=true;
    private char key=' ';

    private HouseService houseService = new HouseService(10);

    //显示主菜单
    public void mainMenu(){
        do {
            System.out.println("========房屋出租系统========");
            System.out.println("\t\t1.新 增 房 源");
            System.out.println("\t\t2.查 找 房 源");
            System.out.println("\t\t3.删 除 房 屋 信 息");
            System.out.println("\t\t4.修 改 房 屋 信 息");
            System.out.println("\t\t5.房 屋 列 表");
            System.out.println("\t\t6.退 出 系 统");
            System.out.print("请输入你的选择（1-6）：");

            key= Utility.readChar();

            switch (key){
                case '1':
                    addHouse();
                    break;
                case '2':
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    exit();
                    break;
            }

        }while (loop);
    }

    //退出系统
    public void exit(){
        char c = Utility.readConfirmSelection();
        if (c=='Y'){
            loop=false;
        }
    }

    //显示房屋列表
    public void listHouse(){
        System.out.println("========房 屋 列 表========");
        System.out.println("编号\t房主\t电话\t地址\t月租\t状态");
        House[] houses=houseService.list();
        for (int i=0;i< houses.length;i++){
            if (houses[i]==null) {
                break;
            }
            System.out.println(houses[i]);
        }
        System.out.println("========房 屋 列 表========\n");
    }

    //添加房屋
    public void addHouse(){
        String name="";
        String phone="";
        String address="";
        int rent=0;
        String stats="";

        System.out.println("========添 加 房 屋========");
        System.out.print("姓名:");
        name=Utility.readString(8);
        System.out.print("电话:");
        phone=Utility.readString(11);
        System.out.print("地址:");
        address=Utility.readString(50);
        System.out.print("租金:");
        rent=Utility.readInt(10);
        System.out.print("状态:");
        stats=Utility.readString(3);
        House newHouse = new House(0,name,phone,address,rent,stats);
        if (houseService.add(newHouse)){
            System.out.println("========添 加 完 成========\n");
            return;
        }
        System.out.println("========添 加 失 败========\n");
    }

    //删除房屋
    public void delHouse(){
        int delId=0;
        char delKey=' ';

        System.out.println("========删 除 房 屋========");
        System.out.print("请输入待删除房屋编号（-1退出）:");
        delId=Utility.readInt();
        if (delId==-1){
            System.out.println("========放 弃 删 除========");
            return;
        }
        delKey=Utility.readConfirmSelection();
        if (delKey=='Y'){
            if(houseService.del(delId)){
                System.out.println("========删 除 成 功========\n");
                return;
            }
            else {
                System.out.println("========删 除 失 败========\n");
                return;
            }
        }
        System.out.println("========放 弃 删 除========\n");
    }

    //查找房屋
    public void findHouse(){
        int findId=-1;
        System.out.println("========查 找 房 屋========");
        System.out.print("请输入待查找房屋编号（-1退出）:");
        findId=Utility.readInt();
        if (findId==-1){
            System.out.println("========放 弃 查 找========");
            return;
        }
        House fHouse = houseService.find(findId);
        if (fHouse!=null){
            System.out.println("编号\t房主\t电话\t地址\t月租\t状态");
            System.out.println(fHouse);
            System.out.println("========查 找 成 功========\n");
            return;
        }
        System.out.println("=====该房屋编号不存在=====");
    }

    //修改房屋
    public void updateHouse(){
        int findId=-1;
        System.out.println("========修 改 房 屋========");
        System.out.print("请输入待修改房屋编号（-1退出）:");
        findId=Utility.readInt();
        if (findId==-1){
            System.out.println("========放 弃 修 改========");
            return;
        }
        House house=houseService.find(findId);
        if (house==null ){
            System.out.println("=====该房屋编号不存在=====");
            return;
        }
        System.out.print("姓名("+house.getName()+"):");
        String name=Utility.readString(8,"");
        if (!"".equals(name)){
            house.setName(name);
        }
        System.out.print("电话("+house.getPhone()+"):");
        String phone=Utility.readString(11,"");
        if (!"".equals(phone)){
            house.setPhone(phone);
        }
        System.out.print("地址("+house.getAddress()+"):");
        String address=Utility.readString(50,"");
        if (!"".equals(address)){
            house.setAddress(address);
        }
        System.out.print("租金("+house.getRent()+"):");
        int rent=Utility.readInt(10);
        if (!"".equals(rent)){
            house.setRent(rent);
        }
        System.out.print("状态("+house.getState()+"):");
        String stats=Utility.readString(3,"");
        if (!"".equals(stats)){
            house.setState(stats);
        }
        System.out.println("========修 改 成 功========\n");
    }
}
